using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Gameplay.PVE.Editor
{
    [CustomEditor(typeof(BunkerHitPointCollider))]
    public class BunkerHitPointColliderEditor : UnityEditor.Editor
    {
        private BunkerHitPointCollider collider { get {  return target  as BunkerHitPointCollider; } }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            if (GUILayout.Button("添加碰撞盒"))
            {
                var c = collider.gameObject.GetOrAddComponent<BoxCollider>();
            }
            collider.luaType = (int) collider.type;
        }
    }
    
    
    [CustomEditor(typeof(BunkerHitPointArea))]
    public class BunkerHitPointAreaEditor : UnityEditor.Editor
    {
        private BunkerHitPointArea area { get {  return target  as BunkerHitPointArea; } }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            if (GUILayout.Button("添加边框"))
            {
                area.CreateBounds();
            }
            if (GUILayout.Button("自动生成点"))
            {
                area.AutoCreateBunkerPoints();
            }

            area.luaType = (int) area.type;
        }
    }
    
    [CustomEditor(typeof(BunkerSingleHitPoint))]
    public class BunkerSingleHitPointEditor : UnityEditor.Editor
    {
        private BunkerSingleHitPoint point { get {  return target  as BunkerSingleHitPoint; } }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            if (GUILayout.Button("自动吸附"))
            {
                BunkerHitPoint.AutoFix(point.transform);
            }
            point.luaType = (int) point.type;
        }
    }
    
    [CustomEditor(typeof(BunkerHitPoint))]
    public class BunkerHitPointEditor : UnityEditor.Editor
    {
        private BunkerHitPoint point { get {  return target  as BunkerHitPoint; } }
        private bool isAutoFix;

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            if (GUILayout.Button("检测"))
            {
                //FindChildren(point.transform);
                var list = point.gameObject.GetComponentsInChildren<BunkerSingleHitPoint>();
                point.singlePoints = new List<BunkerSingleHitPoint>();
                for (int i = 0; i < list.Length; i++)
                {
                    list[i].luaType = (int) list[i].type;
                    point.singlePoints.Add(list[i]);
                }

                var areas = point.gameObject.GetComponentsInChildren<BunkerHitPointArea>();
                point.areas = new List<BunkerHitPointArea>();
                for (int i = 0; i < areas.Length; i++)
                {
                    areas[i].luaType = (int) areas[i].type;
                    point.areas.Add(areas[i]);
                }

                var colliders = point.gameObject.GetComponentsInChildren<BunkerHitPointCollider>();
                point.colliders = new List<BunkerHitPointCollider>();
                for (int i = 0; i < colliders.Length; i++)
                {
                    colliders[i].luaType = (int) colliders[i].type;
                    point.colliders.Add(colliders[i]);
                }
            }
            if (GUILayout.Button("自动吸附"))
            {
                for (int i = 0; i < point.singlePoints.Count; i++)
                {
                    BunkerHitPoint.AutoFix(point.singlePoints[i].transform);
                }
                for (int i = 0; i < point.areas.Count; i++)
                {
                    BunkerHitPoint.AutoFix(point.areas[i]);
                }
            }
        }
        

        private void FindChildren(Transform trans)
        {
            for (int i = 0; i < trans.childCount; i++)
            {
                var child = trans.GetChild(i);
                if (child.name.Contains("[BunkerHitPoint]"))
                {
                    //list.Add(child);
                    child.gameObject.GetOrAddComponent<BunkerSingleHitPoint>();
                }
                FindChildren(child);
            }
        }

        private void OnSceneGUI()
        {
            for (int i = 0; i < point.singlePoints.Count; i++)
            {
                if (point.singlePoints[i])
                {
                    Handles.Label(point.singlePoints[i].transform.position,i.ToString());
                }
            }
            
        }
        
    }
}